package bb.lanxing.calc.processer;

import bb.lanxing.calc.data.DisplayPoint;
import bb.lanxing.calc.data.SourcePoint;
import bb.lanxing.model.database.Trackpoint;
import bb.lanxing.model.database.Workout;
import bb.lanxing.util.CommonUtil;
import bb.lanxing.util.Log;
import java.util.ArrayList;
import java.util.List;

public class SimulatorWorkoutProcessor extends BaseWorkoutProcessor {
    final static private String TAG = "SimulatorWorkoutProcessor";
    private static final int MIN_STORE_NUM = 50;
    private final List<Trackpoint> locationList = new ArrayList<>();

    @Override
    public boolean checkPoint(SourcePoint sourcePoint) {
        if (CommonUtil.isPointValid(sourcePoint)) {
            this.sourcePointPair.update(sourcePoint);
            if (isPaused(sourcePoint)) {
                this.workout.setWorkStatus(Workout.STATUS_PAUSED);//18
                return false;
            }
            return true;
        }
        return false;
    }

    @Override
    public DisplayPoint process(SourcePoint sourcePoint) {
        if (checkPoint(sourcePoint)) {
            calcPoint(sourcePoint);
            calcWorkout(sourcePoint);
            storePoint(sourcePoint);
            return null;
        }
        return null;
    }

    @Override
    public void storePoint(SourcePoint sourcePoint) {
        this.locationList.add(makeTrackpoint(this.workout, sourcePoint));
        if (this.locationList.size() >= MIN_STORE_NUM) {
            Trackpoint.savePoints(this.locationList);
            this.locationList.clear();
            this.workout.save();
            Log.d(TAG, "storeLocationIfNeed, workout.save() 2");
        }
    }

    @Override
    public void flushPoint(boolean z) {
        if (this.locationList.size() > 0) {
            Trackpoint.savePoints(this.locationList);
            this.locationList.clear();
        }
    }
}
